package cn.itlaobing.dao;

import java.sql.*;

import static cn.itlaobing.util.Constants.*;

public class DBhelper<T> {
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	static {
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	private void openConnection()throws ClassNotFoundException,SQLException
	{
		if(conn==null||conn.isClosed())
		{
			conn=DriverManager.getConnection(URL, USER, PWD);
		}	
	}
	public  void close()throws SQLException
	{
		if(rs!=null)rs.close();
		if(ps!=null)ps.close();
		if(conn!=null)conn.close();
	}
	public ResultSet queryExcute(String sql,Object...values)throws ClassNotFoundException,SQLException
	{
		openConnection();
		ps=conn.prepareStatement(sql);
		paramter(values);
		return ps.executeQuery();
		
	}
	public int updateExcute(String sql,Object...values)throws ClassNotFoundException,SQLException
	{
		openConnection();
		ps=conn.prepareStatement(sql);
		paramter(values);
		return ps.executeUpdate();
		
	}
	private void paramter(Object...values)throws SQLException
	{
		if(values.length>0&&values!=null)
		{
			for(int i=0;i<values.length;i++)
			{
				ps.setObject(i+1, values[i]);
			}
		}
	}
}
